package com.situ.mall.controller;

import com.situ.common.util.PageResult;
import com.situ.common.util.Result;
import com.situ.mall.constant.RedisConstants;
import com.situ.mall.pojo.entity.Category;
import com.situ.mall.pojo.query.CategoryQuery;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IProductService productService;

    @RequestMapping("/listAll")
    public Result listAll() {
        List<Category> list = categoryService.listAll();
        return Result.ok(list);
    }
    @RequestMapping("/ByCategoryPatientId")
    public Result ByCategoryPatientId(Integer id) {
        List<Category> list = categoryService.ByCategoryPatientId(id);
        return Result.ok(list);
    }
    @RequestMapping("/selectByPage")
    public PageResult selectByPage(CategoryQuery categoryQuery) {
        PageResult pageInfo = categoryService.selectByPage(categoryQuery);
        return pageInfo;
    }
    @RequestMapping("/addParent")
    public Result addParent(Category category) {
        Integer count = categoryService.countName(category);
        if (count == 0) {
            category.setParentId(0);
            categoryService.addParent(category);
            redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, category.getImage());
            return Result.ok("添加成功");
        } else {
            return Result.error("该分类已经存在");
        }

    }
    @RequestMapping("/addChildren")
    public Result addChildren(Category category) {
        Integer count = categoryService.countNameByParentId(category);
        if (count == 0) {
            categoryService.addChildren(category);
            redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, category.getImage());
            return Result.ok("添加成功");
        } else {
            return Result.error("该分类已经存在");
        }

    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Category category) {
        categoryService.update(category);
        return Result.ok("修改成功");
    }
    @RequestMapping("/updateIsDeleted")
    @ResponseBody
    public Result updateIsDeleted(Category category) {
        categoryService.update(category);
        return Result.ok("修改成功");
    }
    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        Category category = categoryService.selectById(id);
        return Result.ok(category);
    }
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Category category) {
        categoryService.update(category);
        redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, category.getImage());
        return Result.ok("修改成功！");
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        Category category = categoryService.selectById(id);
        Integer parentId = category.getParentId();
        Integer categoryId = category.getId();
        if (parentId == 0) {
            Integer countByParentId = categoryService.countByParentId(categoryId);
            if (countByParentId == 0) {
                categoryService.deleteById(id);
                return Result.ok("删除成功！");
            } else {
                return Result.error("删除失败，该一级分类下还有二级分类！");
            }
        } else {
            Integer countProductByCategoryId = productService.countProductByCategoryId(categoryId);
            if (countProductByCategoryId == 0) {
                categoryService.deleteById(id);
                return Result.ok("删除成功！");
            } else {
                return Result.error("删除失败，该分类下还有商品！");
            }
        }
        //categoryService.deleteById(id);
        //return Result.ok("删除成功！");
    }
}
